/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.peer;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.easymock.classextension.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.ourgrid.acceptance.util.PeerAcceptanceUtil;
import org.ourgrid.acceptance.util.WorkerAcceptanceUtil;
import org.ourgrid.acceptance.util.WorkerAllocation;
import org.ourgrid.acceptance.util.peer.Req_010_Util;
import org.ourgrid.acceptance.util.peer.Req_011_Util;
import org.ourgrid.acceptance.util.peer.Req_019_Util;
import org.ourgrid.acceptance.util.peer.Req_020_Util;
import org.ourgrid.acceptance.util.peer.Req_025_Util;
import org.ourgrid.acceptance.util.peer.Req_036_Util;
import org.ourgrid.acceptance.util.peer.Req_101_Util;
import org.ourgrid.acceptance.util.peer.Req_106_Util;
import org.ourgrid.acceptance.util.peer.Req_108_Util;
import org.ourgrid.acceptance.util.peer.Req_112_Util;
import org.ourgrid.common.interfaces.LocalWorkerProviderClient;
import org.ourgrid.common.interfaces.RemoteWorkerProviderClient;
import org.ourgrid.common.interfaces.Worker;
import org.ourgrid.common.interfaces.control.PeerControl;
import org.ourgrid.common.interfaces.control.PeerControlClient;
import org.ourgrid.common.interfaces.management.WorkerManagement;
import org.ourgrid.common.interfaces.to.LocalWorkerState;
import org.ourgrid.common.interfaces.to.RequestSpec;
import org.ourgrid.common.interfaces.to.UserInfo;
import org.ourgrid.common.interfaces.to.UserState;
import org.ourgrid.common.interfaces.to.WorkerInfo;
import org.ourgrid.common.interfaces.to.WorkerStatus;
import org.ourgrid.common.spec.job.JobSpec;
import org.ourgrid.common.spec.worker.WorkerSpec;
import org.ourgrid.deployer.xmpp.XMPPAccount;
import org.ourgrid.discoveryservice.DiscoveryServiceConstants;
import org.ourgrid.peer.PeerComponent;
import org.ourgrid.reqtrace.ReqTest;

import br.edu.ufcg.lsd.commune.CommuneRuntimeException;
import br.edu.ufcg.lsd.commune.container.ObjectDeployment;
import br.edu.ufcg.lsd.commune.container.logging.CommuneLogger;
import br.edu.ufcg.lsd.commune.functionaltests.util.TestStub;
import br.edu.ufcg.lsd.commune.identification.ContainerID;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;
import br.edu.ufcg.lsd.commune.test.AcceptanceTestUtil;

public class Req_025_Test extends PeerAcceptanceTestCase {

	public static final String COMM_FILE_PATH = "req_025"+File.separator;
	   
	private WorkerAcceptanceUtil workerAcceptanceUtil = new WorkerAcceptanceUtil(getComponentContext());
    private Req_010_Util req_010_Util = new Req_010_Util(getComponentContext());
    private Req_011_Util req_011_Util = new Req_011_Util(getComponentContext());
    private Req_019_Util req_019_Util = new Req_019_Util(getComponentContext());
    private Req_020_Util req_020_Util = new Req_020_Util(getComponentContext());
    private Req_025_Util req_025_Util = new Req_025_Util(getComponentContext());
    private Req_101_Util req_101_Util = new Req_101_Util(getComponentContext());
    private Req_108_Util req_108_Util = new Req_108_Util(getComponentContext());
    private Req_036_Util req_036_Util = new Req_036_Util(getComponentContext());
    private Req_106_Util req_106_Util = new Req_106_Util(getComponentContext());
    private Req_112_Util req_112_Util = new Req_112_Util(getComponentContext());
    private PeerComponent component;

    @Before
	public void setUp() throws Exception{
		super.setUp();
		
        component = req_010_Util.startPeer();
	}
	
    /**
     * Verifies if the peer advert an idle worker.
     */
	@ReqTest(test="AT-025.1", reqs="REQ025")
	@Test public void test_AT_025_1_ChangeStatusIDLE() throws Exception{
		//Set worker A
        WorkerSpec workerSpec = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");
	    List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpec);
        req_010_Util.setWorkers(component, workers);
	    
	    //Notify worker A recovery
        String workerAPublicKey = "publicKeyA";
        DeploymentID workerOID = req_019_Util.notifyWorkerRecovery(component, workerSpec, workerAPublicKey);
	    
	    //Notify nodewiz peer recovery
		DeploymentID dsID = new DeploymentID(new ContainerID("magoDosNos", "sweetleaf.lab", DiscoveryServiceConstants.MODULE_NAME),
				DiscoveryServiceConstants.DS_OBJECT_NAME);
        req_020_Util.notifyDiscoveryServiceRecovery(component, dsID);
        
        //Change worker status to IDLE
        req_025_Util.changeWorkerStatusToIdleWithAdvert(component, workerSpec, workerOID, dsID);

        //Get peer's local workers status and expect worker A to be marked as IDLE
        WorkerInfo workerInfoA = new WorkerInfo(workerSpec, LocalWorkerState.IDLE, null);
        List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
		req_036_Util.getLocalWorkersStatus(localWorkersInfo);
	}

    /**
     * Verifies if the peer doesn't adverts an unknown worker.
     */
	@ReqTest(test="AT-025.2", reqs="REQ025")
	@Test public void test_AT_025_2_ChangeStatusUnknownWorker() throws Exception{
	    //Notify nodewiz peer recovery
		DeploymentID dsID = new DeploymentID(new ContainerID("magoDosNos", "sweetleaf.lab", DiscoveryServiceConstants.MODULE_NAME),
				DiscoveryServiceConstants.DS_OBJECT_NAME);
		
        req_020_Util.notifyDiscoveryServiceRecovery(component, dsID);
    
        //Forge changing worker status to IDLE
        String workerAPublicKey = "publicKeyA";
		DeploymentID workerID = new DeploymentID(new ContainerID("u1", "s1", "m1", workerAPublicKey), "o1");
        
		req_025_Util.changeUnknowWorkerStatusToIdle(component, workerID, workerAPublicKey);
		
        //Get peer's local workers status and expect worker A to do not appear
        List<WorkerInfo> localWorkersInfo = new LinkedList<WorkerInfo>();
        req_036_Util.getLocalWorkersStatus(localWorkersInfo);
	}

	/**
	 * Verifies if the peer stop advertising an ownered worker. 
     * @throws Exception
     */
    @ReqTest(test="AT-025.3", reqs="REQ025")
    @Test public void test_AT_025_3_ChangeStatusIDLE_to_OWNER() throws Exception{
    	
    	//Set worker A
        String workerAUserName = "workerA.ourgrid.org";
        String workerAServerName = "xmpp.ourgrid.org";
        WorkerSpec workerSpec = workerAcceptanceUtil.createWorkerSpec(workerAUserName, workerAServerName);
	    List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpec);
        req_010_Util.setWorkers(component, workers);
	    
        //Notify worker A recovery
        String workerAPublicKey = "publicKeyA";
        DeploymentID workerID = req_019_Util.notifyWorkerRecovery(component, workerSpec, workerAPublicKey);
    	
        //Notify nodewiz peer recovery
		DeploymentID dsID = new DeploymentID(new ContainerID("magoDosNos", "sweetleaf.lab", DiscoveryServiceConstants.MODULE_NAME),
				DiscoveryServiceConstants.DS_OBJECT_NAME);
        req_020_Util.notifyDiscoveryServiceRecovery(component, dsID);
        
        //Change worker status to IDLE
        req_025_Util.changeWorkerStatusToIdleWithAdvert(
        		component, workerSpec, workerID, dsID);
        
        //Change worker status to OWNER and expect to cancel advert
        req_025_Util.changeWorkerStatusToOwner(component, workerID);
        
        //Get peer's local workers status and expect worker A to be marked as OWNER
        WorkerInfo workerInfoA = new WorkerInfo(workerSpec, LocalWorkerState.OWNER, null);
        List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
		req_036_Util.getLocalWorkersStatus(localWorkersInfo);
    }
    
    /**
	 * Validate worker's status changes:
	 * 	A unknown local worker can not change status;
	 * 	A unknown remote worker can not change status;
     * @throws Exception
     */
    @ReqTest(test="AT-025.4", reqs="REQ025")
    @Test public void test_AT_025_4_InputValidation() throws Exception{
    	String unknowPubKey = "unknownWorkerPublicKey";
    	
    	CommuneLogger loggerMock = EasyMock.createMock(CommuneLogger.class);
    	component.setLogger(loggerMock);
    	
    	DeploymentID unknownID = new DeploymentID(new ContainerID("a", "a", "a", unknowPubKey), "a");
    	
    	//A unknown local worker change status to IDLE - expect to log the warn
    	req_025_Util.changeUnknowWorkerStatusToIdle(component, unknownID, unknowPubKey);
    	
		//A unknown remote worker change status to ALLOCATED FOR MYGRID - expect to log the warn
		EasyMock.reset(loggerMock);
		loggerMock.warn(
				"An unknown worker changed its status to Allocated for Broker. It will be ignored. Worker public key: "
				+ unknowPubKey);
		EasyMock.replay(loggerMock);
		req_112_Util.unknownRemoteWorkerStatusChanged("", "", unknowPubKey, component);
		
		EasyMock.verify(loggerMock);
    }
    
    /** 
     * Test change worker public key, during worker failure
     * @throws Exception
     */
   @ReqTest(test="AT-025.5", reqs="REQ025")
   @Test public void test_AT_025_5_PubKeyChange() throws Exception{
	   //Set worker A
	   WorkerSpec workerSpec = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpec);
	   req_010_Util.setWorkers(component, workers);
		
	   //Notify worker A recovery
       String workerAPublicKey = "publicKeyA";
       DeploymentID workerOID = req_019_Util.notifyWorkerRecovery(component, workerSpec, workerAPublicKey);
	   
       //Change worker status to IDLE
       req_025_Util.changeWorkerStatusToIdle(component, workerOID);
       
       //Notify worker failure
       req_019_Util.notifyWorkerFailure(workerOID, component);
       
       //Notify worker recovery with the new public key
       String workerASecondPublicKey = "workerANewPublicKey";
       workerOID = req_019_Util.notifyWorkerRecovery(component, workerSpec, workerASecondPublicKey);
       
       //Change worker status to IDLE with the new public key - expect to log the information
       req_025_Util.changeWorkerStatusToIdle(component, workerOID);
   }
   
   /** 
	* Verifies if the peer give the worker to consumer, when the worker's state changes to allocated for MyGrid.
    */
   @ReqTest(test="AT-025.6", reqs="REQ025")
   @Test public void test_AT_025_6_Idle_to_WorkForMyGrid() throws Exception{
	   //Create an user account
	   XMPPAccount user1 = req_101_Util.createLocalUser("user011", "server011", "011011");
	   
	   //Set worker A
	   WorkerSpec workerSpec = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpec);
	   req_010_Util.setWorkers(component, workers);
		
	   //Notify worker A recovery
       String workerAPublicKey = "publicKeyA";
       DeploymentID workerOID = req_019_Util.notifyWorkerRecovery(component, workerSpec, workerAPublicKey);
	   
       //Change worker status to IDLE
       req_025_Util.changeWorkerStatusToIdle(component, workerOID);
       
       //Login with a valid user
       String mygridPubKey = "myGridPublicKey";
       
       PeerControl peerControl = peerAcceptanceUtil.getPeerControl();
       ObjectDeployment pcOD = peerAcceptanceUtil.getPeerControlDeployment();
	
       PeerControlClient peerControlClient = EasyMock.createMock(PeerControlClient.class);
	
       DeploymentID pccID = new DeploymentID(new ContainerID("pcc", "broker", "broker"), mygridPubKey);
       AcceptanceTestUtil.publishTestObject(component, pccID, peerControlClient, PeerControlClient.class);
	
       AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID);
	
       try {
    	   peerControl.addUser(peerControlClient, user1.getUsername() + "@" + user1.getServerAddress(), user1.getUserpassword());
       } catch (CommuneRuntimeException e) {
    	   //do nothing - the user is already added.
       }
		
       DeploymentID lwpcOID = req_108_Util.login(component, user1, mygridPubKey);
       LocalWorkerProviderClient lwpc = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpcOID);
       
       //Request a worker for the logged user
       WorkerAllocation allocationWorker = new WorkerAllocation(workerOID);
       RequestSpec requestSpec1 = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
       req_011_Util.requestForLocalConsumer(component, new TestStub(lwpcOID, lwpc), requestSpec1, allocationWorker);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpcOID.getServiceID()));
       
       //Change worker status to ALLOCATED FOR MYGRID
       req_025_Util.changeWorkerStatusToAllocatedForBroker(component, lwpcOID, workerOID, workerSpec, requestSpec1);
       
       //Verify if the client was marked as CONSUMING
       UserInfo userInfo1 = new UserInfo(user1.getUsername(), user1.getServerAddress(), mygridPubKey, UserState.CONSUMING);
       List<UserInfo> usersInfo = AcceptanceTestUtil.createList(userInfo1);
       req_106_Util.getUsersStatus(usersInfo);

       //Verify if the worker A was marked as IN_USE
       WorkerInfo workerInfoA = new WorkerInfo(workerSpec, LocalWorkerState.IN_USE, lwpcOID.getServiceID());
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /** 
	* Verifies if the peer ignore a worker status change, when the worker reference is null.
    */
   @ReqTest(test="AT-025.7", reqs="REQ025")
   @Test public void test_AT_025_7_Idle_to_WorkForMyGrid_NullWorker() throws Exception{
	   //Create an user account
	   XMPPAccount user1 = req_101_Util.createLocalUser("user011", "server011", "011011");

	   //Set worker A
	   WorkerSpec workerSpec = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpec);
	   req_010_Util.setWorkers(component, workers);
		
	   //Notify worker A recovery
       String workerAPublicKey = "publicKeyA";
       DeploymentID workerOID = req_019_Util.notifyWorkerRecovery(component, workerSpec, workerAPublicKey);
	   
       //Change worker status to IDLE
       req_025_Util.changeWorkerStatusToIdle(component, workerOID);
       
       //Login with a valid user
       String mygridPubKey = "myGridPublicKey";
       
       PeerControl peerControl = peerAcceptanceUtil.getPeerControl();
       ObjectDeployment pcOD = peerAcceptanceUtil.getPeerControlDeployment();
	
       PeerControlClient peerControlClient = EasyMock.createMock(PeerControlClient.class);
	
       DeploymentID pccID = new DeploymentID(new ContainerID("pcc", "broker", "broker"), mygridPubKey);
       AcceptanceTestUtil.publishTestObject(component, pccID, peerControlClient, PeerControlClient.class);
	
       AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID);
	
       try {
    	   peerControl.addUser(peerControlClient, user1.getUsername() + "@" + user1.getServerAddress(), user1.getUserpassword());
       } catch (CommuneRuntimeException e) {
    	   //do nothing - the user is already added.
       }
		
       DeploymentID lwpcOID = req_108_Util.login(component, user1, mygridPubKey);
       LocalWorkerProviderClient lwpc = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpcOID);

       //Request a worker for the logged user
       WorkerAllocation allocationWorker = new WorkerAllocation(workerOID);
       RequestSpec requestSpec1 = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
       req_011_Util.requestForLocalConsumer(component, new TestStub(lwpcOID, lwpc), requestSpec1, allocationWorker);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpcOID.getServiceID()));
       
       //Change worker status to ALLOCATED FOR MYGRID
       CommuneLogger loggerMock = EasyMock.createMock(CommuneLogger.class);
       component.setLogger(loggerMock);
       loggerMock.warn("Worker <"+ workerOID +"> changed its status to ALLOCATED_FOR_BROKER, but it did not" +
       		" provide a worker reference. This status change was ignored.");
       
       EasyMock.replay(loggerMock);
       req_025_Util.changeWorkerStatusToAllocatedForMyGrid(component, null, workerOID,
    		   (WorkerManagement) AcceptanceTestUtil.getBoundObject(workerOID));
       EasyMock.verify(loggerMock);
   }
   
   /** 
    * Verifies the peer behavior, when it commands the worker to work for MyGrid, but the worker
    * answer with a status changed different of Allocated for MyGrid.
    * 
    * Allocated for peer -> log a warn;
    * Idle -> log a warn;
    * Stopped -> ignore this message, for the peer is about to receive a notification of worker failure.
    * Owner -> mark the worker as OWNER and remove the allocation data;
    */
   @ReqTest(test="AT-025.8", reqs="REQ025")
   @Test public void test_AT_025_8_Idle_to_WorkForMyGrid() throws Exception{
	   //Create an user account
	   XMPPAccount user1 = req_101_Util.createLocalUser("user011", "server011", "011011");
	   
	   //Set worker A
	   WorkerSpec workerSpec = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpec);
	   req_010_Util.setWorkers(component, workers);
		
	   //Notify worker A recovery
       String workerAPublicKey = "publicKeyA";
       DeploymentID workerOID = req_019_Util.notifyWorkerRecovery(component, workerSpec, workerAPublicKey);
	   
       //Change worker status to IDLE
       req_025_Util.changeWorkerStatusToIdle(component, workerOID);
	   
       //Login with a valid user
       String mygridPubKey = "myGridPublicKey";
       
       PeerControl peerControl = peerAcceptanceUtil.getPeerControl();
       ObjectDeployment pcOD = peerAcceptanceUtil.getPeerControlDeployment();
	
       PeerControlClient peerControlClient = EasyMock.createMock(PeerControlClient.class);
	
       DeploymentID pccID = new DeploymentID(new ContainerID("pcc", "broker", "broker"), mygridPubKey);
       AcceptanceTestUtil.publishTestObject(component, pccID, peerControlClient, PeerControlClient.class);
	
       AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID);
	
       try {
    	   peerControl.addUser(peerControlClient, user1.getUsername() + "@" + user1.getServerAddress(), user1.getUserpassword());
       } catch (CommuneRuntimeException e) {
    	   //do nothing - the user is already added.
       }
       
       DeploymentID lwpcOID = req_108_Util.login(component, user1, mygridPubKey);
       LocalWorkerProviderClient lwpc = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpcOID);

       //Request a worker for the logged user
       WorkerAllocation allocation = new WorkerAllocation(workerOID);
       RequestSpec requestSpec1 = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
       req_011_Util.requestForLocalConsumer(component, new TestStub(lwpcOID, lwpc), requestSpec1, allocation);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpcOID.getServiceID()));

       //Create mocks
       CommuneLogger loggerMock = EasyMock.createMock(CommuneLogger.class);
       component.setLogger(loggerMock);
       Worker worker = EasyMock.createMock(Worker.class);
       EasyMock.replay(worker);

       //Change worker status to ALLOCATED FOR PEER
       loggerMock.warn("The worker <" + workerOID + "> (IN_USE) changed its status to ALLOCATED_FOR_PEER. " +
       		"This status change was ignored.");
       EasyMock.replay(loggerMock);
       
       req_025_Util.changeStatusAllocatedForPeer(null, workerOID, component);
       EasyMock.verify(loggerMock);
       EasyMock.reset(loggerMock);
       
       //Verify if the worker A was marked as IN_USE
       WorkerInfo workerInfoA = new WorkerInfo(workerSpec, LocalWorkerState.IN_USE, lwpcOID.getServiceID());
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);

       //Change worker status to IDLE
       loggerMock.debug("Worker <" + workerOID + "> is now IDLE");
       EasyMock.replay(loggerMock);

       req_025_Util.changeStatus(component, WorkerStatus.IDLE, workerOID);
       EasyMock.verify(loggerMock);
       EasyMock.reset(loggerMock);
       
       //Verify if the worker A was marked as IN_USE
       workerInfoA = new WorkerInfo(workerSpec, LocalWorkerState.IDLE, null);
       localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
       
       //Change worker status to STOPPED
       req_025_Util.changeStatus(component, WorkerStatus.STOPPED, workerOID);

       //Verify if the worker A was marked as IN_USE
       workerInfoA = new WorkerInfo(workerSpec, LocalWorkerState.IDLE, null);
       localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
       
       //Change worker status to OWNER
       loggerMock.info("Worker <"+ workerOID +"> is now OWNER");
       EasyMock.replay(loggerMock);
       
       req_025_Util.changeStatus(component, WorkerStatus.OWNER, workerOID);
       EasyMock.verify(loggerMock);
       
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnLocalWorker(workerOID.getServiceID()));
       
       //Verify if the worker A was marked as OWNER
       workerInfoA = new WorkerInfo(workerSpec, LocalWorkerState.OWNER);
       localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /**
	* Verifies if the peer ignore the worker's state changes to idle, when it 
	* did not command the worker to stop working. 
    */
   @ReqTest(test="AT-025.9", reqs="REQ025")
   @Test public void test_AT_025_9_Changing_status_to_Idle_before_StopWorking() throws Exception{
	   //Create an user account
	   XMPPAccount user1 = req_101_Util.createLocalUser("user011", "server011", "011011");
	   
	   //Set one worker
	   WorkerSpec workerSpec = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpec);
	   req_010_Util.setWorkers(component, workers);
		
	   //Notify worker A recovery
       String workerAPublicKey = "publicKeyA";
       DeploymentID workerOID = req_019_Util.notifyWorkerRecovery(component, workerSpec, workerAPublicKey);
	   
       //Change worker status to IDLE
       req_025_Util.changeWorkerStatusToIdle(component, workerOID);
	   
       //Login with a valid user
       String mygridPubKey = "myGridPublicKey";
       
       PeerControl peerControl = peerAcceptanceUtil.getPeerControl();
       ObjectDeployment pcOD = peerAcceptanceUtil.getPeerControlDeployment();
	
       PeerControlClient peerControlClient = EasyMock.createMock(PeerControlClient.class);
	
       DeploymentID pccID = new DeploymentID(new ContainerID("pcc", "broker", "broker"), mygridPubKey);
       AcceptanceTestUtil.publishTestObject(component, pccID, peerControlClient, PeerControlClient.class);
	
       AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID);
	
       try {
    	   peerControl.addUser(peerControlClient, user1.getUsername() + "@" + user1.getServerAddress(), user1.getUserpassword());
       } catch (CommuneRuntimeException e) {
    	   //do nothing - the user is already added.
       }
       
       DeploymentID lwpcOID = req_108_Util.login(component, user1, mygridPubKey);
       LocalWorkerProviderClient lwpc = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpcOID);
       
       //Request a worker for the logged user
       WorkerAllocation allocation = new WorkerAllocation(workerOID);
       RequestSpec requestSpec1 = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
       req_011_Util.requestForLocalConsumer(component, new TestStub(lwpcOID, lwpc), requestSpec1, allocation);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpcOID.getServiceID()));
       
       //Change worker status to ALLOCATED FOR MYGRID
       req_025_Util.changeWorkerStatusToAllocatedForBroker(component, lwpcOID, workerOID, workerSpec, 
    			   requestSpec1);
       
       //Change worker status to IDLE - expect to log the warn
       CommuneLogger loggerMock = EasyMock.createMock(CommuneLogger.class);
       component.setLogger(loggerMock);
       loggerMock.debug("Worker <" + workerOID + "> is now IDLE");
       EasyMock.replay(loggerMock);
       req_025_Util.changeStatus(component, WorkerStatus.IDLE, workerOID);
       
       //Verify if the worker A was marked as IN_USE
       WorkerInfo workerInfoA = new WorkerInfo(workerSpec, LocalWorkerState.IDLE);
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /**
    * Verifies if the peer completes a worker preemption.
    */
   @ReqTest(test="AT-025.10", reqs="REQ025")
   @Test public void test_AT_025_10_WorkerPreemption() throws Exception{
	   //Create two user accounts
	   XMPPAccount user1 = req_101_Util.createLocalUser("user011", "server011", "011011");
	   XMPPAccount user2 = req_101_Util.createLocalUser("user012", "server011", "011011");
	   
	   //Set workers
	   String serverName = "xmpp.ourgrid.org";
	   WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", serverName);
	   WorkerSpec workerSpecB = workerAcceptanceUtil.createWorkerSpec("workerB.ourgrid.org", serverName);
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA, workerSpecB);
	   req_010_Util.setWorkers(component, workers);
	   
	   //Notify workers recovery
       String workerAPublicKey = "workerAPublicKey";
       DeploymentID workerAOID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, workerAPublicKey);
       String workerBPublicKey = "workerBPublicKey";
       DeploymentID workerBOID = req_019_Util.notifyWorkerRecovery(component, workerSpecB, workerBPublicKey);

       //Change workers status to IDLE
       req_025_Util.changeWorkerStatusToIdle(component, workerAOID);
       req_025_Util.changeWorkerStatusToIdle(component, workerBOID);
       
       //Login with valid users
       String mygrid1PubKey = "myGrid1PublicKey";
       
       PeerControl peerControl = peerAcceptanceUtil.getPeerControl();
       ObjectDeployment pcOD = peerAcceptanceUtil.getPeerControlDeployment();
	
       PeerControlClient peerControlClient = EasyMock.createMock(PeerControlClient.class);
	
       DeploymentID pccID = new DeploymentID(new ContainerID("pcc", "broker", "broker"), mygrid1PubKey);
       AcceptanceTestUtil.publishTestObject(component, pccID, peerControlClient, PeerControlClient.class);
	
       AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID);
	
       try {
    	   peerControl.addUser(peerControlClient, user1.getUsername() + "@" + user1.getServerAddress(), user1.getUserpassword());
       } catch (CommuneRuntimeException e) {
    	   //do nothing - the user is already added.
       }
       
       DeploymentID lwpc1OID = req_108_Util.login(component, user1, mygrid1PubKey);
       LocalWorkerProviderClient lwpc = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpc1OID);
       
       String mygrid2PubKey = "myGrid2PublicKey";
       
       PeerControlClient peerControlClient2 = EasyMock.createMock(PeerControlClient.class);
	
       DeploymentID pccID2 = new DeploymentID(new ContainerID("pcc2", "broke2r", "broker"), mygrid2PubKey);
       AcceptanceTestUtil.publishTestObject(component, pccID2, peerControlClient2, PeerControlClient.class);
	
       AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID2);
	
       try {
    	   peerControl.addUser(peerControlClient, user2.getUsername() + "@" + user2.getServerAddress(), user2.getUserpassword());
       } catch (CommuneRuntimeException e) {
    	   //do nothing - the user is already added.
       }
       
       DeploymentID lwpc2OID = req_108_Util.login(component, user2, mygrid2PubKey);
       LocalWorkerProviderClient lwpc2 = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpc2OID);
       
       //Request workers for user1
       WorkerAllocation allocationA = new WorkerAllocation(workerAOID);
       WorkerAllocation allocationB = new WorkerAllocation(workerBOID);
       RequestSpec requestSpec1 = new RequestSpec(0, new JobSpec("label"), 1, "", 2, 0, 0);
       req_011_Util.requestForLocalConsumer(component, new TestStub(lwpc1OID, lwpc), requestSpec1, allocationB, allocationA);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpc1OID.getServiceID()));
       
       //Change workers status to ALLOCATED FOR MYGRID
       req_025_Util.changeWorkerStatusToAllocatedForBroker(component, lwpc1OID, workerBOID, workerSpecB, requestSpec1);
       req_025_Util.changeWorkerStatusToAllocatedForBroker(component, lwpc1OID, workerAOID, workerSpecA, requestSpec1);
       
       //Request workers for user2
       allocationA = new WorkerAllocation(workerAOID).addLoserConsumer(lwpc1OID).addLoserRequestSpec(requestSpec1);
       RequestSpec requestSpec2 =new RequestSpec(0, new JobSpec("label"), 2, "", 1, 0, 0);
       req_011_Util.requestForLocalConsumer(component, new TestStub(lwpc2OID, lwpc2), requestSpec2, allocationA);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpc2OID.getServiceID()));
       
       //Change workerA status to ALLOCATED FOR MYGRID
       req_025_Util.changeWorkerStatusToAllocatedForBroker(component, lwpc2OID, workerAOID, workerSpecA, requestSpec2);
       
       //Verify if the workerA was preempted
       WorkerInfo workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.IN_USE, lwpc2OID.getServiceID());
       WorkerInfo workerInfoB = new WorkerInfo(workerSpecB, LocalWorkerState.IN_USE, lwpc1OID.getServiceID());
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA, workerInfoB);

       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /**
    * Verifies if the peer give the worker to consumer, when the worker's state changes to allocated for Peer.
    */
   @ReqTest(test="AT-025.11", reqs="REQ025")
   @Test public void test_AT_025_11_IdleToAllocatedForPeer() throws Exception{
	   
	   PeerAcceptanceUtil.copyTrustFile(COMM_FILE_PATH+"025_blank.xml");
	   
	   //Set one worker
	   WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");
	   
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA);
	   req_010_Util.setWorkers(component, workers);
	   
	   //Notify worker recovery
       String workerAPublicKey = "workerAPublicKey";
       DeploymentID workerAOID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, workerAPublicKey);
       
       //Change worker status to IDLE
       req_025_Util.changeWorkerStatusToIdle(component, workerAOID);
       
       //Request a worker for the remote client
       DeploymentID clientID = PeerAcceptanceUtil.createRemoteConsumerID("client", "server", "pubKeyA");
       RequestSpec requestSpec = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
       WorkerAllocation allocationWA = new WorkerAllocation(workerAOID);
       RemoteWorkerProviderClient rwpc = req_011_Util.requestForRemoteClient(component, clientID, requestSpec,
    		   										1, allocationWA);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(clientID.getServiceID()));

       //Change worker status to ALLOCATED FOR PEER
       req_025_Util.changeWorkerStatusToAllocatedForPeer(component, rwpc, workerAOID, workerSpecA, clientID);

       //Verify if the worker A was marked as DONATED
       WorkerInfo workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.DONATED, clientID.getServiceID());
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }

   /**
    * Verifies if the peer ignore a worker status change, when the worker reference is null.
    */
   @ReqTest(test="AT-025.12", reqs="REQ025")
   @Test public void test_AT_025_12_IdleToAllocatedForPeerAndWorkerNull() throws Exception{
	   
	   PeerAcceptanceUtil.copyTrustFile(COMM_FILE_PATH+"025_blank.xml");
	   
	   //Set one worker
	   WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");

	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA);
	   req_010_Util.setWorkers(component, workers);

	   //Notify worker recovery
	   String workerAPublicKey = "workerAPublicKey";
	   DeploymentID workerAOID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, workerAPublicKey);
	   WorkerManagement workerManagement = (WorkerManagement) AcceptanceTestUtil.getBoundObject(workerAOID);

	   //Change worker status to IDLE
	   req_025_Util.changeWorkerStatusToIdle(component, workerAOID);

	   //Request a worker for the remote client
	   DeploymentID clientID = PeerAcceptanceUtil.createRemoteConsumerID("client", "server", "pubKeyA");
	   WorkerAllocation allocationWA = new WorkerAllocation(workerAOID);
	   RequestSpec requestSpec = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
	   req_011_Util.requestForRemoteClient(component, clientID, requestSpec, 1, allocationWA);
	   assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(clientID.getServiceID()));
   
       CommuneLogger loggerMock = EasyMock.createMock(CommuneLogger.class);
       component.setLogger(loggerMock);
       loggerMock.warn("Worker <" + workerAOID + "> changed its status to ALLOCATED_FOR_PEER, " +
       		"but it did not provide a worker reference. This status change was ignored.");

       EasyMock.replay(loggerMock);
       replayActiveMocks();
       
       AcceptanceTestUtil.publishTestObject(component, workerAOID, workerManagement, WorkerManagement.class);

       req_025_Util.changeStatusAllocatedForPeer(null, workerAOID, component);
       EasyMock.verify(loggerMock);
       verifyActiveMocks();
   }
   
   /**
    * Verifies the peer behavior, when it commands the worker to work for Peer, 
    * but the worker answer with a status changed different of Allocated for Peer.
    * Allocated for MyGrid -> log a warn;
    * Idle -> log a warn;
    * Stopped -> ignore this message, for the peer is about to receive a notification of worker failure.
    * Owner -> mark the worker as OWNER and remove the allocation data;
    */
   @ReqTest(test="AT-025.13", reqs="REQ025")
   @Test public void test_AT_025_13_IdleToAllocatedForPeerAndOtherStates() throws Exception{
	   
	   PeerAcceptanceUtil.copyTrustFile(COMM_FILE_PATH+"025_blank.xml");
	   
	   //Set one worker
	   WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");

	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA);
	   req_010_Util.setWorkers(component, workers);

	   //Notify worker recovery
	   String workerAPublicKey = "workerAPublicKey";
	   DeploymentID workerAOID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, workerAPublicKey);

	   //Change worker status to IDLE
	   req_025_Util.changeWorkerStatusToIdle(component, workerAOID);

	   //Request a worker for the remote client
	   DeploymentID clientID = PeerAcceptanceUtil.createRemoteConsumerID("client", "server", "pubKeyA");
	   WorkerAllocation allocationWA = new WorkerAllocation(workerAOID);
	   RequestSpec requestSpec = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
	   req_011_Util.requestForRemoteClient(component, clientID, requestSpec, 1, allocationWA);
	   assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(clientID.getServiceID()));

	   //Change worker status to ALLOCATED FOR MYGRID
	   Worker worker = getMock(NOT_NICE, Worker.class);
	   peerAcceptanceUtil.createStub(worker, Worker.class, workerAOID);

	   CommuneLogger loggerMock = EasyMock.createMock(CommuneLogger.class);
	   component.setLogger(loggerMock);
	   loggerMock.warn("The worker <" + workerAOID+ "> (DONATED) changed its status to ALLOCATED_FOR_MYGRID. " +
	   		"This status change was ignored.");

	   EasyMock.replay(loggerMock);
	   replayActiveMocks();

	   req_025_Util.changeWorkerStatusToAllocatedForMyGrid(component, worker, workerAOID,
			   (WorkerManagement) AcceptanceTestUtil.getBoundObject(workerAOID));
	   EasyMock.verify(loggerMock);
	   verifyActiveMocks();
	   resetActiveMocks();
	   EasyMock.reset(loggerMock);
	   
	   //Change worker status to IDLE
	   loggerMock.debug("Worker <" + workerAOID + "> is now IDLE");
	   EasyMock.replay(loggerMock);
	   replayActiveMocks();

	   req_025_Util.changeStatus(component, WorkerStatus.IDLE, workerAOID);
	   EasyMock.verify(loggerMock);
	   verifyActiveMocks();
	   resetActiveMocks();
	   EasyMock.reset(loggerMock);
	   
	   WorkerInfo workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.IDLE, null);
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
	   
	   //Change worker status to STOPPED
	   req_025_Util.changeStatus(component, WorkerStatus.STOPPED, workerAOID);
	  
	   //Verify if the worker A was marked as DONATED
	   new WorkerInfo(workerSpecA, LocalWorkerState.IDLE, null);
       localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
	   
	   //Change worker status to OWNER
	   req_025_Util.changeStatus(component, WorkerStatus.OWNER, workerAOID);
	   
	   //Verify if the worker A was marked as OWNER
	   workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.OWNER);
       localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /**
	*Verifies if the peer ignore the worker's state changes to idle, when it did not command the worker to stop working.
    */
   @ReqTest(test="AT-025.14", reqs="REQ025")
   @Test public void test_AT_025_14_DonatedWorkerChangeStatusToIdle() throws Exception{
	   //Set one worker
	   WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", "xmpp.ourgrid.org");
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA);
	   req_010_Util.setWorkers(component, workers);

	   String workerAPublicKey = "workerAPublicKey";
	   //Notify worker recovery
	   DeploymentID workerAOID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, workerAPublicKey);

	   //Change worker status to IDLE
	   req_025_Util.changeWorkerStatusToIdle(component, workerAOID);

	   //Request a worker for the remote client
	   DeploymentID clientID = PeerAcceptanceUtil.createRemoteConsumerID("client", "server", "pubKeyA");
	   WorkerAllocation allocationWA = new WorkerAllocation(workerAOID);
	   RequestSpec requestSpec = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
	   RemoteWorkerProviderClient rwpc = req_011_Util.requestForRemoteClient(component, clientID, requestSpec, 1, allocationWA);
	   assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(clientID.getServiceID()));

       //Change worker status to ALLOCATED FOR PEER
	   req_025_Util.changeWorkerStatusToAllocatedForPeer(component, rwpc, workerAOID, workerSpecA, clientID);
       
	   //Change worker status to IDLE - expect to log the warn
	   CommuneLogger loggerMock = EasyMock.createMock(CommuneLogger.class);
	   component.setLogger(loggerMock);
	   loggerMock.debug("Worker <" + workerAOID + "> is now IDLE");
	   EasyMock.replay(loggerMock);
       replayActiveMocks();
       req_025_Util.changeStatus(component, WorkerStatus.IDLE, workerAOID);
	   EasyMock.verify(loggerMock);
	   verifyActiveMocks();
	   resetActiveMocks();
	   
	   //Verify if the worker A was marked as DONATED
	   WorkerInfo workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.IDLE, null);
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /**
    * Verifies if the peer completes a worker preemption.
    * @throws Exception
    */
   @ReqTest(test="AT-025.15", reqs="REQ025")
   @Test public void test_AT_025_15_AllocateForPeerPreemption() throws Exception{
	   
	   PeerAcceptanceUtil.copyTrustFile(COMM_FILE_PATH+"025_blank.xml");
	   
	   //Set workers
	   String serverName = "xmpp.ourgrid.org";
	   WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", serverName);
	   WorkerSpec workerSpecB = workerAcceptanceUtil.createWorkerSpec("workerB.ourgrid.org", serverName);
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA, workerSpecB);
	   req_010_Util.setWorkers(component, workers);

	   //Notify workers recovery
	   DeploymentID workerAOID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, "workerAPublicKey");
	   DeploymentID workerBOID = req_019_Util.notifyWorkerRecovery(component, workerSpecB, "workerBPublicKey");
	   
	   //Change workers status to IDLE
	   req_025_Util.changeWorkerStatusToIdle(component, workerAOID);
	   req_025_Util.changeWorkerStatusToIdle(component, workerBOID);
	   
	   //Request two workers for the remote client
	   DeploymentID clientID = PeerAcceptanceUtil.createRemoteConsumerID("client", "server", "pubKeyA");
	   WorkerAllocation allocationWA = new WorkerAllocation(workerAOID);
	   WorkerAllocation allocationWB = new WorkerAllocation(workerBOID);
	   RequestSpec requestSpec = new RequestSpec(0, new JobSpec("label"), 1, "", 2, 0, 0);
	   RemoteWorkerProviderClient rwpc = req_011_Util.requestForRemoteClient(component, clientID, requestSpec, 1, allocationWB, allocationWA);
	   assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(clientID.getServiceID()));
	   
	   //Change workers status to ALLOCATED FOR PEER
	   req_025_Util.changeWorkerStatusToAllocatedForPeer(component, rwpc, workerAOID, workerSpecA, clientID);
	   
	   //worker B
	   req_025_Util.changeWorkerStatusToAllocatedForPeer(component, rwpc, workerBOID, workerSpecB, clientID);
	   
	   //Request a worker for user2
	   DeploymentID client2ID = PeerAcceptanceUtil.createRemoteConsumerID("client2", "server", "pubKeyB");
	   allocationWA = new WorkerAllocation(workerAOID).addLoserConsumer(clientID);
	   RequestSpec requestSpec2 = new RequestSpec(0, new JobSpec("label"), 2, "", 1, 0, 0);
	   rwpc = req_011_Util.requestForRemoteClient(component, client2ID, requestSpec2, 
			   										Req_011_Util.DO_NOT_LOAD_SUBCOMMUNITIES, allocationWA);
	   assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(client2ID.getServiceID()));

	   //Change workerB status to ALLOCATED FOR PEER
	   req_025_Util.changeWorkerStatusToAllocatedForPeer(component,rwpc, workerAOID, workerSpecA, client2ID);
	   
	   //Verify if the workerB was preempted
	   WorkerInfo workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.DONATED, client2ID.getServiceID());
	   WorkerInfo workerInfoB = new WorkerInfo(workerSpecB, LocalWorkerState.DONATED, clientID.getServiceID());
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA, workerInfoB);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /**
    * Verifies if the peer completes a worker preemption.
    */
   @ReqTest(test="AT-025.16", reqs="REQ025")
   @Test public void test_AT_025_16_WorkerPreemptionPeerToMyGrid() throws Exception{
	   //Create an user account
	   XMPPAccount user1 = req_101_Util.createLocalUser("user011", "server011", "011011");

	   //Set workers
	   String serverName = "xmpp.ourgrid.org";
	   WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", serverName);
	   WorkerSpec workerSpecB = workerAcceptanceUtil.createWorkerSpec("workerB.ourgrid.org", serverName);
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA,workerSpecB);
	   req_010_Util.setWorkers(component, workers);

	   //Notify workers recovery
	   String workerAPublicKey = "workerAPublicKey";
	   DeploymentID workerAOID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, workerAPublicKey);
	   DeploymentID workerBOID = req_019_Util.notifyWorkerRecovery(component, workerSpecB, "workerBPublicKey");
	   
	   //Change workers status to IDLE
	   req_025_Util.changeWorkerStatusToIdle(component, workerAOID);
	   req_025_Util.changeWorkerStatusToIdle(component, workerBOID);
	   
	   //Request two workers for the remote client
	   DeploymentID clientID = PeerAcceptanceUtil.createRemoteConsumerID("client", "server", "pubKeyA");
	   WorkerAllocation allocationWA = new WorkerAllocation(workerAOID);
	   WorkerAllocation allocationWB = new WorkerAllocation(workerBOID);
	   RequestSpec requestSpec = new RequestSpec(0, new JobSpec("label"), 1, "", 2, 0, 0);
	   RemoteWorkerProviderClient rwpc = 
		   req_011_Util.requestForRemoteClient(component, clientID, requestSpec, 1, allocationWB, allocationWA);
	   assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(clientID.getServiceID()));
	
	   //Change workers status to ALLOCATED FOR PEER
	   req_025_Util.changeWorkerStatusToAllocatedForPeer(component, rwpc, workerAOID, workerSpecA, clientID);
	   
	   //worker B
	   req_025_Util.changeWorkerStatusToAllocatedForPeer(component, rwpc, workerBOID, workerSpecB, clientID);
	   
	   //Login with a valid user
	   String mygrid1PubKey = "publicKeyL";
	   
	   PeerControl peerControl = peerAcceptanceUtil.getPeerControl();
		ObjectDeployment pcOD = peerAcceptanceUtil.getPeerControlDeployment();
		
		PeerControlClient peerControlClient = EasyMock.createMock(PeerControlClient.class);
		
		DeploymentID pccID = new DeploymentID(new ContainerID("pcc", "broker", "broker"), mygrid1PubKey);
		AcceptanceTestUtil.publishTestObject(component, pccID, peerControlClient, PeerControlClient.class);
		
		AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID);
		
		try {
			peerControl.addUser(peerControlClient, user1.getUsername() + "@" + user1.getServerAddress(), user1.getUserpassword());
		} catch (CommuneRuntimeException e) {
			//do nothing - the user is already added.
		}

	   
       DeploymentID lwpc1OID = req_108_Util.login(component, user1, mygrid1PubKey);
       LocalWorkerProviderClient lwpc = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpc1OID);
       
       //Request workers for user1
       WorkerAllocation allocationA = new WorkerAllocation(workerAOID).addLoserConsumer(clientID);
       RequestSpec requestSpec2 = new RequestSpec(0, new JobSpec("label"), 2, "", 1, 0, 0);
       req_011_Util.requestForLocalConsumer(component, new TestStub(lwpc1OID, lwpc), requestSpec2, allocationA);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpc1OID.getServiceID()));
       
       //Change workers status to ALLOCATED FOR MYGRID
       req_025_Util.changeWorkerStatusToAllocatedForBroker(component, lwpc1OID, workerAOID, workerSpecA, requestSpec2);

       //Verify if the workerA was preempted
	   WorkerInfo workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.IN_USE, lwpc1OID.getServiceID());
	   WorkerInfo workerInfoB = new WorkerInfo(workerSpecB, LocalWorkerState.DONATED, clientID.getServiceID());
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA, workerInfoB);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /**
    * Verifies if the peer remove the allocation data of worker whose status changed to OWNER.
    */
   @ReqTest(test="AT-025.17", reqs="REQ025")
   @Test public void test_AT_025_17_WorkerIDLE_INUSE_DONATED_TO_OWNER() throws Exception{
	   //Create an user account
	   XMPPAccount user1 = req_101_Util.createLocalUser("user011", "server011", "011011");

	   //Set workers
	   String serverName = "xmpp.ourgrid.org";
	   WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("workerA.ourgrid.org", serverName);
	   WorkerSpec workerSpecB = workerAcceptanceUtil.createWorkerSpec("workerB.ourgrid.org", serverName);
	   WorkerSpec workerSpecC = workerAcceptanceUtil.createWorkerSpec("workerC.ourgrid.org", serverName);
	   List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA,workerSpecB, workerSpecC);
	   req_010_Util.setWorkers(component, workers);

	   //Notify workers recovery
	   String workerAPublicKey = "workerAPublicKey";
	   String workerBPublicKey = "workerBPublicKey";
	   String workerCPublicKey = "workerCPublicKey";
	   DeploymentID workerAOID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, workerAPublicKey);
	   DeploymentID workerBOID = req_019_Util.notifyWorkerRecovery(component, workerSpecB, workerBPublicKey);
	   DeploymentID workerCOID = req_019_Util.notifyWorkerRecovery(component, workerSpecC, workerCPublicKey);
	   
	   //Change workers status to IDLE
	   
	   req_025_Util.changeWorkerStatusToIdle(component, workerAOID);
	   req_025_Util.changeWorkerStatusToIdle(component, workerBOID);
	   req_025_Util.changeWorkerStatusToIdle(component, workerCOID);
	   
	   //Login with a valid user
	   String mygrid1PubKey = "publicKeyL";
	   
	   PeerControl peerControl = peerAcceptanceUtil.getPeerControl();
	   ObjectDeployment pcOD = peerAcceptanceUtil.getPeerControlDeployment();
	
	   PeerControlClient peerControlClient = EasyMock.createMock(PeerControlClient.class);
	
	   DeploymentID pccID = new DeploymentID(new ContainerID("pcc", "broker", "broker"), mygrid1PubKey);
	   AcceptanceTestUtil.publishTestObject(component, pccID, peerControlClient, PeerControlClient.class);
	
	   AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID);
	
	   try {
		   peerControl.addUser(peerControlClient, user1.getUsername() + "@" + user1.getServerAddress(), user1.getUserpassword());
	   } catch (CommuneRuntimeException e) {
		   //do nothing - the user is already added.
	   }
		
       DeploymentID lwpc1OID = req_108_Util.login(component, user1, mygrid1PubKey);
       LocalWorkerProviderClient lwpc = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpc1OID);
       
       //Request workers for user1
       WorkerAllocation allocationC = new WorkerAllocation(workerCOID);
       RequestSpec requestSpec1 = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
       req_011_Util.requestForLocalConsumer(component, new TestStub(lwpc1OID, lwpc), requestSpec1, allocationC);
       assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpc1OID.getServiceID()));
       
       //Change workers status to ALLOCATED FOR MYGRID
       req_025_Util.changeWorkerStatusToAllocatedForBroker(component, lwpc1OID, workerCOID, workerSpecC, requestSpec1);
       
       //Request a worker for the remote client
       resetActiveMocks();
	   DeploymentID clientID = PeerAcceptanceUtil.createRemoteConsumerID("client", "server", "pubKeyA");
	   WorkerAllocation allocationWB = new WorkerAllocation(workerBOID);
	   RequestSpec requestSpec2 = new RequestSpec(0, new JobSpec("label"), 2, "", 1, 0, 0);
	   RemoteWorkerProviderClient rwpc = req_011_Util.requestForRemoteClient(component, clientID, requestSpec2, 1, 
			   allocationWB);
	   assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(clientID.getServiceID()));
	
	   //worker B
	   req_025_Util.changeWorkerStatusToAllocatedForPeer(component, rwpc, workerBOID, workerSpecB, clientID);
	   
	   //Verify workers status
	   WorkerInfo workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.IDLE, null);
	   WorkerInfo workerInfoB = new WorkerInfo(workerSpecB, LocalWorkerState.DONATED, clientID.getServiceID());
	   WorkerInfo workerInfoC = new WorkerInfo(workerSpecC, LocalWorkerState.IN_USE, lwpc1OID.getServiceID());
       List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoC, workerInfoB, workerInfoA);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);

       //Change worker status to OWNER
       req_025_Util.changeWorkerStatusToOwner(component, workerAOID);
       req_025_Util.changeWorkerStatusToOwner(component, workerBOID);
       req_025_Util.changeWorkerStatusToOwner(component, workerCOID);
       
       
       //Verify workers status
	   workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.OWNER, null);
	   workerInfoB = new WorkerInfo(workerSpecB, LocalWorkerState.OWNER, null);
	   workerInfoC = new WorkerInfo(workerSpecC, LocalWorkerState.OWNER, null);
       
	   localWorkersInfo = AcceptanceTestUtil.createList(workerInfoA, workerInfoB, workerInfoC);
       req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
   
   /**
    * Verifies if the peer ignore when a worker change its status to STOPPED.
    */
   @ReqTest(test="AT-025.18", reqs="REQ025")
    @Test public void test_AT_025_18_ChangeToStopped() throws Exception{
	    //Create two user accounts
	    XMPPAccount user1 = req_101_Util.createLocalUser("user011", "server011", "011011");

	    //Set workers
		WorkerSpec workerSpecA = workerAcceptanceUtil.createWorkerSpec("U1", "S1");
		WorkerSpec workerSpecB = workerAcceptanceUtil.createWorkerSpec("U2", "S1");
		WorkerSpec workerSpecC = workerAcceptanceUtil.createWorkerSpec("U3", "S1");
		WorkerSpec workerSpecD = workerAcceptanceUtil.createWorkerSpec("U4", "S1");
		
		List<WorkerSpec> workers = AcceptanceTestUtil.createList(workerSpecA, workerSpecB, workerSpecC, workerSpecD);
		req_010_Util.setWorkers(component, workers);
		
		//Notify workers recovery
		String workerAPubKey = "publicKeyWA";
		DeploymentID workerAObjID = req_019_Util.notifyWorkerRecovery(component, workerSpecA, workerAPubKey);
		String workerBPubKey = "publicKeyWB";
		DeploymentID workerBObjID = req_019_Util.notifyWorkerRecovery(component, workerSpecB, workerBPubKey);
		String workerCPubKey = "publicKeyWC";
		DeploymentID workerCObjID = req_019_Util.notifyWorkerRecovery(component, workerSpecC, workerCPubKey);
		String workerDPubKey = "publicKeyWD";
		DeploymentID workerDOID = req_019_Util.notifyWorkerRecovery(component, workerSpecD, workerDPubKey);
		
		//Change workers status to IDLE
		req_025_Util.changeWorkerStatusToIdle(component, workerAObjID);
		req_025_Util.changeWorkerStatusToIdle(component, workerBObjID);
		req_025_Util.changeWorkerStatusToIdle(component, workerCObjID);
		
		//Change worker status to OWNER
		req_025_Util.changeWorkerStatusToOwner(component, workerDOID);
		
		//Login with a valid user
		String localUserPubKey = "publicKeyL";
		
		PeerControl peerControl = peerAcceptanceUtil.getPeerControl();
		ObjectDeployment pcOD = peerAcceptanceUtil.getPeerControlDeployment();
		
		PeerControlClient peerControlClient = EasyMock.createMock(PeerControlClient.class);
		
		DeploymentID pccID = new DeploymentID(new ContainerID("pcc", "broker", "broker"), localUserPubKey);
		AcceptanceTestUtil.publishTestObject(component, pccID, peerControlClient, PeerControlClient.class);
		
		AcceptanceTestUtil.setExecutionContext(component, pcOD, pccID);
		
		try {
			peerControl.addUser(peerControlClient, user1.getUsername() + "@" + user1.getServerAddress(), user1.getUserpassword());
		} catch (CommuneRuntimeException e) {
			//do nothing - the user is already added.
		}
		
		DeploymentID lwpcOID = req_108_Util.login(component, user1, localUserPubKey);
		LocalWorkerProviderClient lwpc = (LocalWorkerProviderClient) AcceptanceTestUtil.getBoundObject(lwpcOID);

		//Request a worker for the logged user
		/*LocalWorkerProviderClient lwpc = getMock(NOT_NICE, LocalWorkerProviderClient.class);
		peerAcceptanceUtil.createStub(lwpc, lwpcOID);*/

		replayActiveMocks();
		WorkerAllocation workerCAllocation = new WorkerAllocation(workerCObjID);
        RequestSpec requestSpec1 = new RequestSpec(0, new JobSpec("label"), 1, "", 1, 0, 0);
		req_011_Util.requestForLocalConsumer(component, new TestStub(lwpcOID, lwpc), requestSpec1, workerCAllocation);
		
		verifyActiveMocks();
		resetActiveMocks();
		assertTrue(peerAcceptanceUtil.isPeerInterestedOnBroker(lwpcOID.getServiceID()));
		
		//Change worker status to ALLOCATED FOR MYGRID
		req_025_Util.changeWorkerStatusToAllocatedForBroker(component, lwpcOID, workerCObjID, workerSpecC, requestSpec1);

		//Request a worker for the remote client
		DeploymentID clientID = PeerAcceptanceUtil.createRemoteConsumerID("client", "server", "pubKeyA");
		WorkerAllocation allocationWB = new WorkerAllocation(workerBObjID);
		RequestSpec requestSpec2 = new RequestSpec(0, new JobSpec("label"), 2, "", 1, 0, 0);
		RemoteWorkerProviderClient rwpc = req_011_Util.requestForRemoteClient(component, clientID, requestSpec2, 1, 
				allocationWB);
		assertTrue(peerAcceptanceUtil.isPeerInterestedOnRemoteClient(clientID.getServiceID()));

		//Change workers status to ALLOCATED FOR PEER
		req_025_Util.changeWorkerStatusToAllocatedForPeer(component, rwpc, workerBObjID, workerSpecB, clientID);
		
		//Verify workers status
		WorkerInfo workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.IDLE, null);
		WorkerInfo workerInfoB = new WorkerInfo(workerSpecB, LocalWorkerState.DONATED, clientID.getServiceID());
		WorkerInfo workerInfoC = new WorkerInfo(workerSpecC, LocalWorkerState.IN_USE, lwpcOID.getServiceID());
		WorkerInfo workerInfoD = new WorkerInfo(workerSpecD, LocalWorkerState.OWNER, null);
		List<WorkerInfo> localWorkersInfo = AcceptanceTestUtil.createList(workerInfoC, workerInfoB, workerInfoA,  
				workerInfoD);
		req_036_Util.getLocalWorkersStatus(localWorkersInfo);
		
		//Change worker status to STOPPED
		req_025_Util.changeStatus(component, WorkerStatus.STOPPED, workerAObjID);
		req_025_Util.changeStatus(component, WorkerStatus.STOPPED, workerBObjID);
		req_025_Util.changeStatus(component, WorkerStatus.STOPPED, workerCObjID);
		req_025_Util.changeStatus(component, WorkerStatus.STOPPED, workerDOID);
		
		workerInfoA = new WorkerInfo(workerSpecA, LocalWorkerState.IDLE, null);
		workerInfoB = new WorkerInfo(workerSpecB, LocalWorkerState.DONATED, clientID.getServiceID());
		workerInfoC = new WorkerInfo(workerSpecC, LocalWorkerState.IN_USE, lwpcOID.getServiceID());
		workerInfoD = new WorkerInfo(workerSpecD, LocalWorkerState.OWNER, null);
		
		localWorkersInfo = AcceptanceTestUtil.createList(workerInfoC, workerInfoB, workerInfoA, workerInfoD);
		req_036_Util.getLocalWorkersStatus(localWorkersInfo);
   }
}